package com.enjoyor.soa.traffic.server.efoms.dubbo.manage;

import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;

import com.alibaba.dubbo.config.annotation.Service;
import com.enjoyor.soa.traffic.api.dto.efoms.WorkordersInfoDto;
import com.enjoyor.soa.traffic.api.ubms.dto.DeviceInfoDto;
import com.enjoyor.soa.traffic.core.efoms.domain.WorkordersInfo;
import com.enjoyor.soa.traffic.core.efoms.domain.WorkordersRecordInfo;
import com.enjoyor.soa.traffic.core.efoms.enums.EnumDicPublic;
import com.enjoyor.soa.traffic.core.efoms.service.IWorkordersInfoService;
import com.enjoyor.soa.traffic.process.vo.ProcessVo;
import com.enjoyor.soa.traffic.server.efoms.dubbo.invoke.DubboProcessServiceInvoke;
import com.enjoyor.soa.traffic.server.efoms.dubbo.invoke.DubboUbmsServiceInvoke;
import com.enjoyor.soa.traffic.server.efoms.dubbo.listener.MessagePushClient;
import com.enjoyor.soa.traffic.server.efoms.helper.ObjectSwitchHelper;
import com.enjoyor.soa.traffic.util.enums.EnumAppCode;
import com.enjoyor.soa.traffic.util.helper.LoggerHelper;
import com.enjoyor.soa.traffic.util.helper.ResultHelper;
import com.enjoyor.soa.traffic.util.helper.TimeHelper;
import com.enjoyor.soa.traffic.util.pojo.Page;
import com.enjoyor.soa.traffic.util.pojo.ResultPojo;
@Service
public class ManageWorkordersInfoService {

    @Autowired
    private IWorkordersInfoService workordersInfoService;
    
    @Autowired
    private ManageRepairsInfoService manageRepairsInfoService;
    
    @Autowired 
    private ManageDevStatusInfoService manageDevStatusInfoService;
    
	@Autowired
	private DubboUbmsServiceInvoke dubboUbmsServiceInvoke;

	@Autowired
	private DubboProcessServiceInvoke dubboProcessServiceInvoke;
	
	@Autowired
	private MessagePushClient messagePushClient;
	
    public ResultPojo selectWorkordersInfoById(String workordersId){
    	try {
	    	WorkordersInfo workordersInfo = workordersInfoService.selectWorkordersInfoById(workordersId);
	    	WorkordersInfoDto workordersInfoDto = ObjectSwitchHelper.workordersPojoToDto(workordersInfo);
	        return ResultHelper.getResult(workordersInfoDto);
    	} catch (Exception e) {
            LoggerHelper.LOG.error(e);
            return ResultHelper.getResult(e);
        }
    }

	public ResultPojo selectWorkordersInfoList(String workordersId, String repStartDate, String repEndDate,
			String devCategoryCode, String devTypeCode, String devId, String devName, String key, String devAreaCode,
			String devDeptId, String failureTypeCode, String failureDescrible, String repSourceCode,
			String workordersStatusCode, String lableClick){
		try {
			List<WorkordersInfo> workordersInfoList = workordersInfoService.selectWorkordersInfoList(workordersId,
					repStartDate, repEndDate, devCategoryCode, devTypeCode, devId, devName, key, devAreaCode, devDeptId,
					failureTypeCode, failureDescrible, repSourceCode, workordersStatusCode, lableClick);
			List<WorkordersInfoDto> workordersInfoDtoList = ObjectSwitchHelper.workordersPojoToDtoList(workordersInfoList);
			return ResultHelper.getResult(workordersInfoDtoList);
		} catch (Exception e) {
            LoggerHelper.LOG.error(e);
            return ResultHelper.getResult(e);
        }
	}
    
    public ResultPojo selectWorkordersInfoPage(Page page, String workordersId, String repStartDate, String repEndDate,
			String devCategoryCode, String devTypeCode, String devId, String devName, String key, String devAreaCode,
			String devDeptId, String failureTypeCode, String failureDescrible, String repSourceCode,
			String workordersStatusCode, String orderSeq, String orderMode, String lableClick, List<String> noStatusCodeList) {
		try {
	    	Page resultPage = workordersInfoService.selectWorkordersInfoPage(page, workordersId, repStartDate, repEndDate,
					devCategoryCode, devTypeCode, devId, devName, key, devAreaCode, devDeptId, failureTypeCode,
					failureDescrible, repSourceCode, workordersStatusCode, orderSeq, orderMode, lableClick, noStatusCodeList);
	        return ResultHelper.getResult(resultPage);
		} catch (Exception e) {
            LoggerHelper.LOG.error(e);
            return ResultHelper.getResult(e);
        }
    }
	
	public ResultPojo insertWorkordersInfo(WorkordersInfoDto workordersInfoDto, String personName){
    	try {
			String result = "";
	    	WorkordersInfo workordersInfo = ObjectSwitchHelper.workordersDtoToPojo(workordersInfoDto);
	    	String devId = workordersInfoDto.getDevId();
	    	String devTypeCode = workordersInfoDto.getDevTypeCode();
	    	if(null != devId && !"".equals(devId) && null != devTypeCode && !"".equals(devTypeCode)) {
	    		//设备重复检验
	    		String flagCheck = manageRepairsInfoService.devCheck(devTypeCode, devId, personName);
	    		if("1".equals(flagCheck)) {
	    			return ResultHelper.getResult("该设备处于报修中, 自动产生催办记录！");
	    		}else if("2".equals(flagCheck)){
	    			return ResultHelper.getResult("该设备处于维修中, 自动产生催办记录！");
	    		}
	    		//获取设备基础信息
	    		ResultPojo resultPojo = dubboUbmsServiceInvoke.getDevInfoList(devId, devTypeCode);
	    		List<DeviceInfoDto> listDeviceInfoDto = (List<DeviceInfoDto>) resultPojo.getResultList();
	    		if (null != listDeviceInfoDto && listDeviceInfoDto.size() > 0) {
	    			DeviceInfoDto deviceInfoDto = listDeviceInfoDto.get(0);
	    			workordersInfo = ObjectSwitchHelper.UbmsDeviceToWorkordersInfoDto(deviceInfoDto, workordersInfo);
	    		}
	    	}
	    	
	    	String workordersId = "W" + TimeHelper.getUniqueString();
	    	String processId = "";
	    	String workordersStatusCode = "";
	    	if(!workordersInfo.getIsRpeApply()) {
	    		//启动A流程
	    		processId = dubboProcessServiceInvoke.startOrdersAProcess(workordersId);
	    		//执行立案流程
	        	ProcessVo processVo = dubboProcessServiceInvoke.submitProcess(workordersId, null, 
	        			EnumDicPublic.ORD_OPER_SUBMIT.getKey());
	        	if(processVo.getResult()) {
	            	workordersStatusCode = processVo.getTaskDefinitionKey();
	        	}else {
	        		return ResultHelper.getResult(EnumAppCode.WORK_FLOW_ERROR, "流程引擎执行错误！");
	        	}
	        	//判断是否需要派发流程
	        	Map<String,List<WorkordersRecordInfo>> workordersRecordMap = workordersInfo.getWorkordersRecordMap();
	        	if(null != workordersRecordMap) {
	        		List<WorkordersRecordInfo> dispatchList = workordersRecordMap.get("dispatchList");
	            	if(null != dispatchList && dispatchList.size() > 0) {
	            		ProcessVo processVoAgain = dubboProcessServiceInvoke.submitProcess(workordersId, null, 
	                			EnumDicPublic.ORD_OPER_DISPATCH.getKey());
	                	if(processVoAgain.getResult()) {
	                    	workordersStatusCode = processVoAgain.getTaskDefinitionKey();
	                	}else {
	                		return ResultHelper.getResult(EnumAppCode.WORK_FLOW_ERROR, "流程引擎执行错误！");
	                	}
	            	}
	        	}
	    	}else {
	    		//启动B流程
	    		processId = dubboProcessServiceInvoke.startOrdersBProcess(workordersId);
	    		//执行立案流程
	        	ProcessVo processVo = dubboProcessServiceInvoke.submitProcess(workordersId, null, 
	        			EnumDicPublic.ORD_OPER_SUBMIT.getKey());
	        	if(processVo.getResult()) {
	            	workordersStatusCode = processVo.getTaskDefinitionKey();
	        	}else {
	        		return ResultHelper.getResult(EnumAppCode.WORK_FLOW_ERROR, "流程引擎执行错误！");
	        	}
	    	}
	    	
	    	workordersInfo.setWorkordersId(workordersId);
	    	workordersInfo.setProcessId(processId);
	    	workordersInfo.setWorkordersStatusCode(workordersStatusCode);
			result = workordersInfoService.insertWorkordersInfo(workordersInfo, personName);
	    	
			//修改设备状态数据--维修中、故障
			manageDevStatusInfoService.saveDevStatusInfo(
					workordersInfo.getDevCategoryCode(),workordersInfo.getDevCategoryName(),
					workordersInfo.getDevTypeCode(), workordersInfo.getDevTypeName(),
					workordersInfo.getDevId(), workordersInfo.getDevName(), 
					EnumDicPublic.DEV_REP_STATUS_MAINTAINING.getKey(), 
					EnumDicPublic.DEV_REP_STATUS_MAINTAINING.getValue(),
					EnumDicPublic.DEVICE_STATUS_FAILURE.getKey(), 
					EnumDicPublic.DEVICE_STATUS_FAILURE.getValue(),  personName);
	        return ResultHelper.getResult(result);
    	} catch (Exception e) {
            LoggerHelper.LOG.error(e);
            return ResultHelper.getResult(e);
        }
	}
	
	public ResultPojo updateWorkordersInfo(WorkordersInfoDto workordersInfoDto, String personName) {
    	try {
			WorkordersInfo workordersInfo = ObjectSwitchHelper.workordersDtoToPojo(workordersInfoDto);
	    	String result = workordersInfoService.updateWorkordersInfo(workordersInfo, personName);
	        return ResultHelper.getResult(result);
    	} catch (Exception e) {
            LoggerHelper.LOG.error(e);
            return ResultHelper.getResult(e);
        }
    }
    
    public ResultPojo deleteWorkordersInfo(String workordersId, String personName) {
    	try {
	    	String result = workordersInfoService.deleteWorkordersInfo(workordersId, personName);
	        return ResultHelper.getResult(result);
    	} catch (Exception e) {
            LoggerHelper.LOG.error(e);
            return ResultHelper.getResult(e);
        }
    }

	public ResultPojo getOperationCount(String startTime,String endTime) {
		try {
			Map<String,Object> resultMap = workordersInfoService.getOperationCount(startTime,endTime);
			return ResultHelper.getResult(resultMap);
		} catch (Exception e) {
            LoggerHelper.LOG.error(e);
            return ResultHelper.getResult(e);
        }
	}
}
